var scrollObj_redFlowerRank=null;
$( document ).on( "pagecreate", "#redFlowerRank", function( event ) {
	$("#redFlowerRank").on("swipeleft",function(){
		scrollObj_redFlowerRank.destroy();
		$.mobile.changePage(rootPath+"/jsp/weixin/sendFlowerRank.jsp?access_token="+access_token);
	});
	
	
});
$( document ).on( "pageshow", "#redFlowerRank", function( event ) {
	access_token=getQueryString("access_token");
	
	pageNo="1";
	pageSize="10";
//	alert("1 "+$("#wrapper_redFlowerRank  #pullDown"));
	$("#wrapper_redFlowerRank  #pullDown").hide();
	$("#wrapper_redFlowerRank  #pullUp").hide();

	scrollObj_redFlowerRank=scrollPlugLoadedOfReturn_new("wrapper_redFlowerRank",
			function() {
				setTimeout(function () {
					if(pageNo >= totalPage)
						return;
					pageNo++;
					loadData_redFlowerRank(false,scrollObj_redFlowerRank);
//					$("#wrapper_redFlowerRank  #pullDown").show();
				}, 1000);	
			},
			function() {
				setTimeout(function () {
					if(pageNo>1){
						pageNo--;
						loadData_redFlowerRank(true,scrollObj_redFlowerRank);
						if(pageNo==1){
							$("#wrapper_redFlowerRank  #pullDown").hide();
						}
					}
				}, 1000);
			});
	
	setTimeout(function () { loadData_redFlowerRank(true,scrollObj_redFlowerRank); }, 100);
});

/**
 * 
 * @param isFresh 是否刷新
 * @param isFresh 是否是分页加载数据
 */
function loadData_redFlowerRank(isFresh,scrollObj_redFlowerRank){
	showLoading();
	$.ajax({
	  	url: rootPath+"/user/redFlowerRank.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&pageNo="+pageNo+"&pageSize="+pageSize,
	  	success: function( response ) {
	  		hideLoading();
	  		var data=$.parseJSON(response);
	  		
			if(data.state=="200"){
				totalPage=data.data.totalPage;
		  		var totalRow=data.data.totalRow;
		  		var arr=data.data.rows;
		  		
				var content = "";
				for (var i=0;i<arr.length;i++){
					var headerPic=rootPath+defaultHeadPic;
					if(arr[i].headImageUrl!=undefined && arr[i].headImageUrl!="")
						headerPic=arr[i].headImageUrl;
					var toPage=rootPath+"/jsp/weixin/otherPersonInfo.jsp?empId="+arr[i].empId+"&access_token="+access_token;
					var flowerUrl=flower_bloom;
			  		if(arr[i].redflowerState=="死掉"){
			  			flowerUrl=flower_die;
			  		}else if(arr[i].redflowerState=="枯萎"){
			  			flowerUrl=flower_droop;
			  		}else if(arr[i].redflowerState=="盛开"){
			  			flowerUrl=flower_bloom;
			  		}
					var bgPicClass="";
					if(arr[i].id==1)
						bgPicClass="first";
					else if(arr[i].id==2)
						bgPicClass="second";
					else if(arr[i].id==3)
						bgPicClass="thirdly";
					else
						bgPicClass="";
					
			  		flowerUrl=rootPath+flowerUrl;
					content +='<li>\
					  <a href="#" onClick="goTo(\''+toPage+'\')">\
					  	<span class="ui-li-desc li_one '+bgPicClass+' ">'+(arr[i].id)+'</span>\
					  	<span class="ui-li-desc li_two "><img src="'+headerPic+'"  width="40" height="40"></span>\
						<span class="ui-li-desc li_three " >'+(arr[i].emp_name)+'</span>\
						<span class="ui-li-desc li_four " ><img src="'+flowerUrl+'" width="40" height="40"/></span>\
						<span class="ui-li-desc li_five " ><strong>'+(parseInt(arr[i].redFlowerNum)+parseInt(arr[i].fdFlowerNum))+'</strong></span>\
					  </a>\
					</li>';
				}
				if(arr.length<pageSize){
					$("#wrapper_redFlowerRank  #pullUp").hide();
				}else{
					$("#wrapper_redFlowerRank  #pullUp").show();
				}
				if(isFresh && $("#thelist_redFlowerRank").html()!=""){
					$("#thelist_redFlowerRank").html("");
				}
				
//				alert(pageNo%3==0);
				//页数是2的倍数时，清空缓存列表
				if(pageNo%3==0){
					$("#wrapper_redFlowerRank  #pullDown").show();
					$("#thelist_redFlowerRank").html("");
					$("#thelist_redFlowerRank").append(content).listview('refresh');
				}else{
					$("#thelist_redFlowerRank").append(content).listview('refresh');
				}
				scrollObj_redFlowerRank.refresh();
				
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}

